package com.itheima.web.servlet;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Map;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            // 验证码校验
            //1. 获取用户输入的验证码
            String checkCode = request.getParameter("checkCode");
            //2. 获取程序生成, 存储在session中的验证码
            HttpSession session = request.getSession();
            String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER");

            // 为了确保验证码是一次性的, 获取以后务必要清除session的验证码
            session.removeAttribute("CHECKCODE_SERVER");

            //3. 比较两码是否一致
            if (!checkCode.equalsIgnoreCase(checkcode_server)) {
                request.setAttribute("msg", "验证码错误");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
                return;
            }

            //request.setCharacterEncoding("UTF-8");
            //response.setContentType("text/html;charset=UTF-8");

            //1. 获取所有请求参数
            Map<String, String[]> map = request.getParameterMap();

            //2. 将数据封装到对象中 javaBean
            User user = new User();
            BeanUtils.populate(user, map);

            //3. 调用service查询
            UserService service = new UserServiceImpl();
            User existUser = service.login(user);

            //4. 判断结果
            if (existUser != null) {
                // 登录成功
                session.setAttribute("user", existUser);

                // 记住用户名  start
                String rememberMe = request.getParameter("rememberMe");
                if ("yes".equals(rememberMe)) {
                    // 用户勾选了
                    Cookie cookie = new Cookie("rememberMeCookie", user.getUsername());
                    cookie.setMaxAge(24 * 60 * 60);
                    response.addCookie(cookie);
                }
                // 记住用户名  end

                // 自动登录 start
                String autoLogin = request.getParameter("autoLogin");
                if ("yes".equals(autoLogin)) {
                    // 用户勾选了   jack&1234
                    Cookie cookie = new Cookie("autoLoginCookie", user.getUsername() + "&" + user.getPassword());
                    cookie.setMaxAge(24 * 60 * 60);
                    response.addCookie(cookie);
                }
                // 自动登录 end

                // 重定向到网站 success.jsp
                response.sendRedirect(request.getContextPath() + "/index.jsp");
            } else {
                // 登录失败
                request.setAttribute("msg", "用户名或密码错误");
                request.getRequestDispatcher("/login.jsp").forward(request, response);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
